package actionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.springframework.context.ApplicationContext;

import util.DomainUtils;
import domain.dao.interfaces.IDAOUser;

public class AddUserValidationForm extends ActionForm{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static int MINIMAL_LOGIN_LENGTH = 3;
	private static int MINIMAL_PASSWORD_LENGHT = 3;
	
	
	private String login = null;
	private String password = null;
	private String password_confirmation = null;
	
	public String getLogin() {
		return login;
	}
	public void setLogin(String login) {
		this.login = DomainUtils.trim(login);
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getPassword_confirmation() {
		return password_confirmation;
	}
	public void setPassword_confirmation(String password_confirmation) {
		this.password_confirmation = password_confirmation;
	}
	
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		login=null;
		password=null;
		password_confirmation=null;	
	}
	
	
	
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();

		HttpSession session = request.getSession(false);
		
		ApplicationContext context = util.SpringUtils.getApplicationContext();

		//Checking session
		if(session == null) {
			errors.add(
					DomainUtils.ALERT_ERROR_STRING,
					new ActionMessage(
							"adduser.session.error"));
			
			DomainUtils.keepParametersContent(request);
			
			return errors;
			
		}

		if(session.getAttribute(DomainUtils.CURRENT_USER_STRING) != null) {
			errors.add(
					DomainUtils.ALERT_ERROR_STRING,
					new ActionMessage(
							"adduser.connected.error"));
			
			DomainUtils.keepParametersContent(request);
			
			return errors;
		}


		//Checking login
		if(login == null || DomainUtils.startByALetter(login) == false){
			errors.add(
					"login",
					new ActionMessage(
							"adduser.login.startbyaletter.error"));
			
			DomainUtils.keepParametersContent(request);
			
			return errors;
		}

		if(DomainUtils.match(login, "\\w+") == false) {
			errors.add(
					"login",
					new ActionMessage(
							"adduser.login.containnostrangecharacter.error"));
			
			DomainUtils.keepParametersContent(request);
			
			return errors;
		}

		IDAOUser daoUser = (IDAOUser) context.getBean("beanDAOUser");

		if(daoUser.getByLogin(login) != null) {
			errors.add(
					"login",
					new ActionMessage(
							"adduser.login.alreadyexist.error"));
			
			DomainUtils.keepParametersContent(request);
			
			return errors;
		}

		if(login.length() < MINIMAL_LOGIN_LENGTH){
			errors.add(
					"login",
					new ActionMessage(
							"adduser.login.minimum_length.error"));
			
			DomainUtils.keepParametersContent(request);
			
			return errors;
		}


		//Checking password
		if(password == null || password.length() < MINIMAL_PASSWORD_LENGHT){
			errors.add(
					"password",
					new ActionMessage(
							"adduser.password.minimum_length.error"));
			
			DomainUtils.keepParametersContent(request);
			
			return errors;
			
		}

		if(password.equals(password_confirmation) == false) {
			errors.add(
					"password_confirmation",
					new ActionMessage(
							"adduser.password_confirmation.error"));
			
			DomainUtils.keepParametersContent(request);
			
			return errors;
		}
		
		return errors;
	}


}
